package com.example.webmodule.config.cron;

import com.example.webmodule.properties.TaskThreadPoolProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @ClassName EY-SchedulerTaskConfig
 * @Description com.example.webmodule.config.cron
 * @Author menyj
 * @Date 2025/3/19
 **/
@ConditionalOnProperty(name = "ey.task.scan-interface.enabled", havingValue ="true", matchIfMissing = false)
@Configuration
public class SchedulerTaskConfig {

    @Autowired
    private TaskThreadPoolProperties taskThreadPoolProperties;

    @Bean("schedulerThreadPoolTaskExecutor")
    public ThreadPoolTaskExecutor schedulerThreadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 核心线程数
        executor.setCorePoolSize(taskThreadPoolProperties.getCorePoolSize());
        // 最大线程数
        executor.setMaxPoolSize(taskThreadPoolProperties.getMaxPoolSize());
        // 队列容量
        executor.setQueueCapacity(taskThreadPoolProperties.getQueueCapacity());
        // 线程名前缀
        executor.setThreadNamePrefix(taskThreadPoolProperties.getThreadNamePrefix());
        // 拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 初始化
        executor.initialize();

        return executor;
    }
}
